#include "json.h"
#include "json_private.h"

int precision = 3;

string JSON_VAR::display(int ts, bool show_name)
{
    string tab = "";
    for (int i = 0; i < ts; i++)
    {
        tab += "\t";
    }

    string ans;
    ans = tab;
    if (show_name)
    {
        ans += "\"" + name + "\": ";
    }
    if (type == JSON_TNIL)
    {
        return "";
    }
    else if (type == JSON_TSTR)
    {
        ans += "\"" + (*(string *)p) + "\"";
    }
    else if (type == JSON_TNUM)
    {
        string show = nts(*(long double *)p, precision);
        ans += show;
    }
    else if (type == JSON_TBOOL)
    {
        bool val = (*(bool *)p);
        string show;
        if (val)
        {
            show = VTRUE;
        }
        else
        {
            show = VFALSE;
        }
        ans += show;
    }
    else if (type == JSON_TARR)
    {
        ans += "[\n";

        vector<JSON_VAR> &nums = *(JSON_ARR *)p;
        int size = nums.size();
        for (int i = 0; i < size; i++)
        {
            ans += nums[i].display(ts + 1, 0);
            if (i < size - 1)
            {
                ans += ",\n";
            }
        }
        ans += "\n" + tab + "]";
    }
    else if (type == JSON_TOBJ)
    {
        JSON_OBJ *obj = (JSON_OBJ *)p;
        ans += obj->display(ts);
    }
    return ans;
}